<?php

namespace App\Http\Controllers\Admin;

use App\Models\Trees;
use App\Models\UserBubbles;
use App\Models\UserTree;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class GameController extends BaseController
{
    public function trees()
    {
        if ($this->isOnSubmit())
        {
            $delete = $this->request->input('delete');
            if ($delete)
            {
                Trees::whereIn('treeid', $delete)->delete();
            }
            $trees = $this->request->input('trees');
            if ($trees)
            {
                foreach ($trees as $treeid=>$tree)
                {
                    if ($tree['title'])
                    {
                        if ($treeid > 0)
                        {
                            Trees::where('treeid', $treeid)->update($tree);
                        } else {
                            Trees::insert($tree);
                        }
                    }
                }
            }

            return $this->messager()->render();

        } else {

            $items = Trees::all();
            return $this->view('admin.game.trees', ['items'=>$items]);
        }
    }

    public function setTreeImg(Request $request)
    {
        $treeid = $request->input('treeid');
        $image  = $request->input('image');

        Trees::where('treeid', $treeid)->update(['image'=>$image]);
        return ajaxReturn();
    }

    public function userTrees()
    {
        $items = UserTree::with(['user','tree'])->orderByDesc('id')->paginate(20);
        return $this->view('admin.game.usertrees',[
            'items'=>$items,
            'pagination'=>$items->render()
        ]);
    }

    public function delTrees(Request $request)
    {
        $delete = $request->input('delete');
        if ($delete)
        {
            UserTree::whereIn('id', $delete)->delete();
        }

        return ajaxReturn();
    }

    public function Bubbles(Request $request)
    {
        $items = UserBubbles::with(['user'])->orderByDesc('id')->paginate(20);
        return $this->view('admin.game.bubbles',[
            'items'=>$items,
            'pagination'=>$items->render()
        ]);
    }

    public function delBubbles(Request $request)
    {
        $delete = $request->input('delete');
        if ($delete)
        {
            UserBubbles::whereIn('id', $delete)->delete();
        }

        return ajaxReturn();
    }
}
